<?php

namespace App\Http\Controllers\Auth;

use App\Facades\Tx;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */


    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function login(Request $request)
    {
        $this->validate($request, [
            'phone' => 'required',
            'password' => 'required',
        ],[
            'phone.*'=>'请输入手机号',
            'password.*'=>'请输入密码'
        ]);

        $user = User::where('phone',$request->get('phone'))->first();

        if(!$user){
            return Tx::fail('用户不存在');
        }
        if(md5($request->get('password')) != $user->password)
        {
            return Tx::fail('密码错误');
        }
        $user['token'] = $user->createDeviceToken();
        return Tx::success($user);
    }
}
